package com.czxy.tanhua.service;

import com.czxy.tanhua.autoconfig.template.OssTemplate;
import com.czxy.tanhua.autoconfig.template.SmsTemplate;
import com.czxy.tanhua.commons.utils.JwtUtils;
import com.czxy.tanhua.dubbo.api.UserApi;
import com.czxy.tanhua.dubbo.api.UserInfoApi;
import com.czxy.tanhua.entity.User;
import com.czxy.tanhua.entity.UserInfo;
import com.czxy.tanhua.exception.BusinessException;
import com.czxy.tanhua.vo.BlackListVo;
import com.czxy.tanhua.vo.ErrorResult;
import com.czxy.tanhua.vo.ResultList;
import com.czxy.tanhua.vo.SettingsVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/30 9:55
 */
@Service
public class UserService {
    @Resource
    private SmsTemplate smsTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private OssTemplate ossTemplate;

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    /**
     * 登录用户的验证码
     * @param phone
     */
    public void loginUser(String phone) {
        //随机生成验证码
//        String code = RandomStringUtils.randomNumeric(6);
        String code = "123456";
        //调用短信工具类
//        smsTemplate.sendSms(phone,"",code);
        String key = "CHECK_CODE"+phone;
        //存放到redis中
        stringRedisTemplate.opsForValue().set(key,code,Duration.ofMinutes(5));
    }
    /**
     * 登录验证码的校验&用户注册
     * @param map
     * @return
     */
    public Map loginVerification(Map map) {
        String phone = (String) map.get("phone");
        String verificationCode = (String) map.get("verificationCode");

        //将验证码进行校验
        String s = stringRedisTemplate.opsForValue().get("CHECK_CODE" + phone);
        //取出之后删除验证码
        stringRedisTemplate.delete("CHECK_CODE"+phone);

        if (s == null){
            throw new BusinessException(ErrorResult.codeempty());
        }
        if (!s.equals(verificationCode)){
            throw new BusinessException(ErrorResult.codeError());
        }
        //判断用户是否已经注册
        Boolean isNew = false;
        //验证码校验成功,查询数据库是否存在该用户
        User user = userApi.findByMobile(phone);
        if (user == null){
            //新注册用户
            user = new User();
            user.setMobile(phone);
            user.setPassword(DigestUtils.md5Hex("123456"));
            //进行添加到数据库中
            Long id = userApi.save(user);
            System.out.println(id == null);
            user.setId(id);
            isNew = true;
        }
        //号码已经再数据库中存在生成token进行返回
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("id",user.getId());
        tokenMap.put("mobile", user.getMobile());
        String token = JwtUtils.getToken(tokenMap);
        Map<String, Object> result = new HashMap<>();
        result.put("token",token);
        result.put("isNew",isNew);
        return result;
    }

    public UserInfo finduserById(Long userId) {
        UserInfo user = userInfoApi.findUser(userId);
        return user;
    }

    public void editUser(UserInfo userInfo, Long userId) {
        userInfoApi.editUser(userInfo,userId);
    }

    public void editheader(MultipartFile file, Long userId) {
        String upload = ossTemplate.upload(file);
        UserInfo user = userInfoApi.findUser(userId);
        user.setAvatar(upload);
        userInfoApi.update(user);
    }

    public SettingsVo findUsersettings(Long userId) {
        return userApi.findUsersettings(userId);
    }

    public void editquestion(String content, Long id) {
        userApi.editquestion(content,id);
    }

    public void editsetting(SettingsVo settingsVo, Long userId) {
        userApi.editsetting(settingsVo,userId);
    }

    public ResultList findblacklist(BlackListVo blackListVo, Long userId) {
        ResultList findblacklist = userApi.findblacklist(blackListVo,userId);
        return findblacklist;
    }

    public void deleteblack(Long uid) {
        userApi.deleteblack(uid);
    }
}